package com.lf.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lf.sys.mapper.SysMessageJunkMapper;
import com.lf.sys.model.SysMessageJunk;
import com.lf.sys.service.MessageJunkService;

import tk.mybatis.mapper.entity.Example;

/**
 * 用户管理
 */
@Service
public class MessageJunkServiceImp implements MessageJunkService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	public SysMessageJunkMapper sysMessageJunkMapper;

	@Override
	public int insert(SysMessageJunk temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "insert" + " param:" + JSON.toJSONString(temp));
		return sysMessageJunkMapper.insert(temp);
	}

	@Override
	public int delete(long id, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "delete" + " param:" + "id:" + id + "userId:"
				+ userId);
		SysMessageJunk temp = new SysMessageJunk();
		temp.setId(id);
		temp.setUserId(userId);
		return sysMessageJunkMapper.delete(temp);
	}

	@Override
	public int update(SysMessageJunk temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "update" + " param:" + JSON.toJSONString(temp));
		return sysMessageJunkMapper.updateByPrimaryKey(temp);
	}

	@Override
	public SysMessageJunk get(long id, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "get" + " param:" + "id:" + id + "userId:"
				+ userId);
		SysMessageJunk temp = new SysMessageJunk();
		temp.setId(id);
		temp.setUserId(userId);
		return sysMessageJunkMapper.selectOne(temp);
	}

	@Override
	public List<SysMessageJunk> list(long mailId, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "list" + " param:" + "emailId:" + mailId
				+ "userId:" + userId);
		SysMessageJunk temp = new SysMessageJunk();
		temp.setUserId(userId);
		temp.setMailId(mailId);
		return sysMessageJunkMapper.select(temp);
	}

	@Override
	public List<SysMessageJunk> list(SysMessageJunk temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "list" + " param:" + JSON.toJSONString(temp));
		return sysMessageJunkMapper.select(temp);
	}

	@Override
	public int insert(List<SysMessageJunk> temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "insert" + " param:" + JSON.toJSONString(temp));
		return sysMessageJunkMapper.insertList(temp);
	}

	@Override
	public List<SysMessageJunk> listByPage(long userId, long mailId, String colomn, String word, int offset, int limit,
			int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "listByPage" + " param:" + "mailId:" + mailId
				+ "userId:" + userId);
		RowBounds r = new RowBounds(offset, limit);
		Example example = new Example(SysMessageJunk.class);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		if (StringUtils.isNotBlank(colomn) && StringUtils.isNotBlank(word)) {
			colomn = colomn.trim();
			word = "%" + word.trim() + "%";
			if ("subject".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list)
						.andLike("subject", word);
			} else if ("bodyText".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list)
						.andLike("bodyText", word);
			} else if ("mailFrom".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list)
						.andLike("mailFrom", word);
			} else if ("mailAddress".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list)
						.andLike("mailAddress", word);
			} else {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list);
			}
		} else {
			example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list);
		}

		return sysMessageJunkMapper.selectByExampleAndRowBounds(example, r);
	}

	@Override
	public int count(long userId, long mailId, String colomn, String word, int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "count" + " param:" + "mailId:" + mailId
				+ "userId:" + userId);
		Example example = new Example(SysMessageJunk.class);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		if (StringUtils.isNotBlank(colomn) && StringUtils.isNotBlank(word)) {
			colomn = colomn.trim();
			word = "%" + word.trim() + "%";
			if ("subject".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list)
						.andLike("subject", word);
			} else if ("bodyText".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list)
						.andLike("bodyText", word);
			} else if ("mailFrom".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list)
						.andLike("mailFrom", word);
			} else if ("mailAddress".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list)
						.andLike("mailAddress", word);
			} else {
				example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list);
			}
		} else {
			example.and().andEqualTo("userId", userId).andEqualTo("mailId", mailId).andIn("state", list);
		}

		return sysMessageJunkMapper.selectCountByExample(example);
	}

	@Override
	public List<SysMessageJunk> listByPage(long userId, String colomn, String word, int offset, int limit,
			int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "listByPage" + " param:" + "userId:" + userId);
		RowBounds r = new RowBounds(offset, limit);
		Example example = new Example(SysMessageJunk.class);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		if (StringUtils.isNotBlank(colomn) && StringUtils.isNotBlank(word)) {
			colomn = colomn.trim();
			word = "%" + word.trim() + "%";
			if ("subject".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andIn("state", list).andLike("subject", word);
			} else if ("bodyText".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andIn("state", list).andLike("bodyText", word);
			} else if ("mailFrom".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andIn("state", list).andLike("mailFrom", word);
			} else if ("mailAddress".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andIn("state", list).andLike("mailAddress", word);
			} else {
				example.and().andEqualTo("userId", userId).andIn("state", list);
			}
		} else {
			example.and().andEqualTo("userId", userId).andIn("state", list);
		}

		return sysMessageJunkMapper.selectByExampleAndRowBounds(example, r);
	}

	@Override
	public int count(long userId, String colomn, String word, int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "count" + " param:" + "userId:" + userId);
		Example example = new Example(SysMessageJunk.class);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		if (StringUtils.isNotBlank(colomn) && StringUtils.isNotBlank(word)) {
			colomn = colomn.trim();
			word = "%" + word.trim() + "%";
			if ("subject".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andIn("state", list).andLike("subject", word);
			} else if ("bodyText".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andIn("state", list).andLike("bodyText", word);
			} else if ("mailFrom".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andIn("state", list).andLike("mailFrom", word);
			} else if ("mailAddress".equalsIgnoreCase(colomn)) {
				example.and().andEqualTo("userId", userId).andIn("state", list).andLike("mailAddress", word);
			} else {
				example.and().andEqualTo("userId", userId).andIn("state", list);
			}
		} else {
			example.and().andEqualTo("userId", userId).andIn("state", list);
		}

		return sysMessageJunkMapper.selectCountByExample(example);
	}
}
